//
// Created by denglibin on 19-10-29.
//
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <ctype.h>
#include <strings.h>
#include <arpa/inet.h>
#include <string.h>

#define CLIENT_PORT 9000
static int  main_(){
    int lfd;
    char buf[1024];
    char server_ip[128];
    socklen_t  addr_len;
    struct sockaddr_in  local_addr, server_addr;
    addr_len = sizeof(local_addr);

    lfd = socket(AF_INET, SOCK_DGRAM, 0);

    bzero(&local_addr, addr_len);
    local_addr.sin_family = AF_INET;
    local_addr.sin_port = htons(CLIENT_PORT);
    local_addr.sin_addr.s_addr = htonl(INADDR_ANY);

    /*绑定地址端口*/
    bind(lfd, (struct sockaddr*)&local_addr, sizeof(local_addr));

    while (1) {
        bzero(server_ip, sizeof(server_ip));
        bzero(buf, sizeof(buf));
        /*接收数据*/
        recvfrom(lfd, buf, sizeof(buf), 0, (struct sockaddr*)&server_addr, &addr_len);
        printf("server: %s:%d ",
               inet_ntop(AF_INET, &server_addr.sin_addr.s_addr, server_ip, sizeof(server_ip)),
               ntohs(server_addr.sin_port));
        printf("%s\n", buf);
    }

    return 0;
}